package top.yaofengqiao.springcloudsimple.system.biz.trigger.http.admin;

import org.springframework.web.bind.annotation.*;
import top.yaofengqiao.springcloudsimple.common.constant.ResponseCode;
import top.yaofengqiao.springcloudsimple.common.util.PageInfo;
import top.yaofengqiao.springcloudsimple.common.util.PageParam;
import top.yaofengqiao.springcloudsimple.common.util.R;
import top.yaofengqiao.springcloudsimple.system.biz.domain.dict.entity.DictEntity;
import top.yaofengqiao.springcloudsimple.system.biz.domain.dict.service.IDictService;
import top.yaofengqiao.springcloudsimple.system.biz.trigger.http.admin.dto.DictDataDTO;
import top.yaofengqiao.springcloudsimple.system.biz.trigger.http.admin.dto.DictTypeDTO;
import top.yaofengqiao.springcloudsimple.system.biz.trigger.http.admin.vo.DictDataVO;
import top.yaofengqiao.springcloudsimple.system.biz.trigger.http.admin.vo.DictTypeVO;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author yfq
 * @date 14:26
 * @description
 */
@RestController
@RequestMapping("/admin/dict")
public class AdminDictController {
    @Resource
    private IDictService dictService;

    @PostMapping("/queryDictTypes")
    public R<PageInfo<DictTypeVO>> queryDictTypes(@RequestBody DictTypeDTO dictTypeDTO, PageParam pageParam) {
        DictEntity dictEntity = DictEntity.builder()
                .type(dictTypeDTO.getType())
                .name(dictTypeDTO.getName())
                .build();
        PageInfo<DictEntity> pageInfo = dictService.queryDictTypes(dictEntity,pageParam);

        return R.ok(pageInfo.convert(o -> DictTypeVO.builder()
                .id(o.getId())
                .name(o.getName())
                .type(o.getType())
                .status(o.getStatus())
                .remark(o.getRemark())
                .build()));
    }

    @GetMapping("/queryDictEntityById/{id}")
    public R<DictTypeVO> queryDictEntityById(@PathVariable("id") long id) {
        DictEntity dictEntity = dictService.queryDictEntityById(id);

        return R.ok(DictTypeVO.builder()
                .id(dictEntity.getId())
                .name(dictEntity.getName())
                .type(dictEntity.getType())
                .status(dictEntity.getStatus())
                .remark(dictEntity.getRemark())
                .values(dictEntity.getValues()
                        .stream()
                        .map(o -> DictDataVO.builder()
                                .id(o.getId())
                                .sort(o.getSort())
                                .label(o.getLabel())
                                .value(o.getValue())
                                .dictType(o.getDictType())
                                .status(o.getStatus())
                                .colorType(o.getColorType())
                                .cssClass(o.getCssClass())
                                .remark(o.getRemark())
                                .build())
                        .collect(Collectors.toList()))
                .build());
    }

    @PostMapping("/updateOrSaveDictType")
    public R<Void> updateDictType(@RequestBody DictTypeDTO dictTypeDTO) {
        DictEntity dictEntity = DictEntity.builder()
                .id(dictTypeDTO.getId())
                .type(dictTypeDTO.getType())
                .name(dictTypeDTO.getName())
                .status(dictTypeDTO.getStatus())
                .remark(dictTypeDTO.getRemark())
                .build();

        boolean flag = dictTypeDTO.getId() == null ?
                dictService.saveDictType(dictEntity) : dictService.updateDictType(dictEntity);

        return flag ? R.ok() : R.fail(ResponseCode.UN_ERROR);
    }

    @PostMapping("/updateOrSaveDictData")
    public R<Void> updateDictData(@RequestBody DictDataDTO dictDataDTO) {
        DictEntity.Value value = DictEntity.Value.builder()
                .id(dictDataDTO.getId())
                .sort(dictDataDTO.getSort())
                .label(dictDataDTO.getLabel())
                .value(dictDataDTO.getValue())
                .status(dictDataDTO.getStatus())
                .colorType(dictDataDTO.getColorType())
                .cssClass(dictDataDTO.getCssClass())
                .remark(dictDataDTO.getRemark())
                .build();
        boolean flag = dictDataDTO.getId() == null ?
                dictService.saveDictData(value) : dictService.updateDictData(value);

        return flag ? R.ok() : R.fail(ResponseCode.UN_ERROR);
    }

}
